package com.gy.service.impl;


import com.aaa.entity.CareOrderItem;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gy.feign.DispenseFeign;
import com.gy.mapper.CareOrderItemMapper;
import com.gy.mapper.OrderChargeItemMapper;
import com.gy.service.DispenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Author GaoYa
 * @Date 2023/4/4 18:01
 * @Version 1.5
 */
@Service
public class DispenseServiceImpl implements DispenseService {
    @Autowired
    private CareOrderItemMapper careOrderMapper;

    @Autowired
    private OrderChargeItemMapper orderChargeItemMapper;

    @Autowired
    private CareOrderItemMapper careOrderItemMapper;

    @Autowired
    private DispenseFeign dispenseFeign;

    /**
     * 处方发药并从库存中减去
     * @param itemIds
     * @return
     */
    @Override
    public Result DispenseOut(String[] itemIds) {
        for (int i = 0; i <itemIds.length ; i++) {
            orderChargeItemMapper.updateByType(itemIds[i]);
            careOrderItemMapper.updateStatusByType(itemIds[i]);
        }
        for (int i = 0; i < itemIds.length; i++) {
            QueryWrapper<CareOrderItem> queryWrap = new QueryWrapper<>();
            queryWrap.eq("item_id", itemIds[i]);
            CareOrderItem careOrders = careOrderMapper.selectOne(queryWrap);
            BigDecimal num = careOrders.getNum();
            String itemName = careOrders.getItemName();
            dispenseFeign.updByDispense(String.valueOf(num), itemName);
        }
        return new Result(2000, "发药成功", true);
    }
}
